﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ChoDiaOc_HK_WPF.Models;
using ChoDiaOc_HK_WPF.Controls;

namespace ChoDiaOc_HK_WPF.Views.ChupAnh {
    /// <summary>
    /// Interaction logic for DangKyDiaOc.xaml
    /// </summary>
    public partial class DangKyDiaOc : UserControl {

        private DIAOC diaOcSource;

        public DIAOC DiaOcSource {
            get { return diaOcSource; }
            set { diaOcSource = value; }
        }

        private LOAIDIAOC loaiDiaOcSource;

        public LOAIDIAOC LoaiDiaOcSource {
            get { return loaiDiaOcSource; }
            set { loaiDiaOcSource = value; }
        }

        private List<LOAIDIAOC> listLoaiDiaOc = null;

        public List<LOAIDIAOC> ListLoaiDiaOc {
            get {
                if (listLoaiDiaOc == null) {
                    listLoaiDiaOc = DatabaseWrapper.GetInstance().GetAllLoaiDiaOc();
                }
                return listLoaiDiaOc;
            }
            set { listLoaiDiaOc = value; }
        }

        public DangKyDiaOc() {
            InitializeComponent();

            loaiDiaOcSource = (LOAIDIAOC)this.FindResource("loaiDiaOcSource");
            diaOcSource = (DIAOC)this.FindResource("diaOcSource");

            TiepNhanDangKyController.GetInstance().DiaOcSource = diaOcSource;
            TiepNhanDangKyController.GetInstance().LoaiDiaOcSource = loaiDiaOcSource;

            List<String> listTenLoaiDiaOc = ListLoaiDiaOc.Select(t => t.TenLoaiDiaOc).ToList();
            tenLoaiDiaOcComboBox.ItemsSource = listTenLoaiDiaOc;

            ResetSource();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e) {

        }

        private void tenLoaiDiaOcComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            String selected = (sender as ComboBox).SelectedItem as String;
            TiepNhanDangKyController.GetInstance().LoaiDiaOcSource = loaiDiaOcSource = ListLoaiDiaOc.Where(t => t.TenLoaiDiaOc == selected).Single();
            kyHieuTextBox.Text = loaiDiaOcSource.KyHieu;
        }

        private void btnSinhMa_Click(object sender, RoutedEventArgs e) {
            maDiaOcTextBox.Text = TiepNhanDangKyController.GetInstance().GenerateMaDiaOc();
        }

        public void btnChinhSua_Click(object sender, RoutedEventArgs e) {
            App.ManHinhTiepNhanDangKy.Step = 1;
            App.ManHinhTiepNhanDangKy.DoNextStep(1);
            // Lock up
            btnSinhMa.IsEnabled = true;
            diaChiTextBox.IsReadOnly = false;
            dienTichDatTextBox.IsReadOnly = false;
            dienTichXayDungTextBox.IsReadOnly = false;
            tenLoaiDiaOcComboBox.IsEnabled = true;
            huongTextBox.IsReadOnly = false;
            viTriTextBox.IsReadOnly = false;
            moTaTextBox.IsReadOnly = false;
            trangThaiTextBox.IsReadOnly = false;
            giaBanTextBox.IsReadOnly = false;
            btnChinhSua.IsEnabled = false;
            btnHoanTat.IsEnabled = true;
            btnNhapLai.IsEnabled = true;

            //App.ManHinhTiepNhanDangKy.DangKyQuangCaoControl
        }

        private void btnNhapLai_Click(object sender, RoutedEventArgs e) {
            ResetSource();
        }

        private void btnHoanTat_Click(object sender, RoutedEventArgs e) {
            Validate();
            if (isValidated) {
                App.ManHinhTiepNhanDangKy.DoNextStep(2);
                // Lock up
                btnSinhMa.IsEnabled = false;
                diaChiTextBox.IsReadOnly = true;
                dienTichDatTextBox.IsReadOnly = true;
                dienTichXayDungTextBox.IsReadOnly = true;
                tenLoaiDiaOcComboBox.IsEnabled = false;
                huongTextBox.IsReadOnly = true;
                viTriTextBox.IsReadOnly = true;
                moTaTextBox.IsReadOnly = true;
                trangThaiTextBox.IsReadOnly = true;
                giaBanTextBox.IsReadOnly = true;
                btnChinhSua.IsEnabled = true;
                btnHoanTat.IsEnabled = false;
                btnNhapLai.IsEnabled = false;
                MessageBox.Show("Thông tin hợp lệ!", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK, MessageBoxOptions.None);
            } else {
                MessageBox.Show("Thông tin không hợp lệ!", "Cảnh báo", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK, MessageBoxOptions.None);
            }
        }

        public void ResetSource() {
            diaOcSource.DiaChi = diaOcSource.MaDiaOc = diaOcSource.MaLoaiDiaOc = diaOcSource.MoTa = diaOcSource.TrangThai = diaOcSource.ViTri = diaOcSource.Huong = "";
            diaOcSource.DienTichDat = diaOcSource.DienTichXayDung = 0;
            diaOcSource.GiaBan = "0";
            btnChinhSua.IsEnabled = false;
            btnNhapLai.IsEnabled = true;
            btnHoanTat.IsEnabled = true;
            loaiDiaOcSource.MaLoaiDiaOc = "";
            tenLoaiDiaOcComboBox.SelectedIndex = -1;
            kyHieuTextBox.Text = "";
            trangThaiTextBox.Text = "Rao bán";
            diaOcSource.TrangThai = "Rao bán";
        }

        private bool isValidated = false;

        public bool IsValidated {
            get { return isValidated; }
            set { isValidated = value; }
        }

        public void Validate() {
            if (diaOcSource.MaDiaOc == "" || loaiDiaOcSource.MaLoaiDiaOc == "" || diaOcSource.MoTa == "" || double.Parse(diaOcSource.GiaBan) <= 0 || diaOcSource.Huong == "" || diaOcSource.DiaChi == "" ||
                diaOcSource.DienTichDat <= 0 || diaOcSource.DienTichXayDung <= 0 || diaOcSource.ViTri == "") {
                isValidated = false;
                return;
            }

            if (diaOcSource.DienTichDat < diaOcSource.DienTichXayDung) {
                isValidated = false;
                return;
            }

            isValidated = true;
        }

        private void btnAidMoney_Click(object sender, RoutedEventArgs e) {
            double tmp = Double.Parse(diaOcSource.GiaBan);
            tmp *= 1000.0;
            diaOcSource.GiaBan = tmp.ToString();
            giaBanTextBox.Text = tmp.ToString();
        }

        private void button1_Click(object sender, RoutedEventArgs e) {
            double tmp = Double.Parse(diaOcSource.GiaBan);
            tmp *= 1000000000.0;
            diaOcSource.GiaBan = tmp.ToString();
            giaBanTextBox.Text = tmp.ToString();
        }
    }
}
